package com.hl.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.SysArticle;
import com.hl.sfzy.mapper.SysArticleMapper;
import com.hl.sfzy.service.SysArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysArticleService")
public class SysArticleServiceImpl implements SysArticleService {
    @Autowired
    private SysArticleMapper articleMapper;

    @Override
    public int add(SysArticle entity) {
        return articleMapper.insert(entity);
    }

    @Override
    public int edit(SysArticle entity) {
        return articleMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return articleMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return articleMapper.deleteBatch(ids);
    }

    @Override
    public List<SysArticle> find(SysArticle entity) {
        return articleMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize,
                                        SysArticle entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysArticle> list = articleMapper.select(entity);
        PageInfo<SysArticle> pageInfo = new PageInfo<>(list);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        pageMap.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return pageMap;
    }

    @Override
    public SysArticle findById(Integer id) {
        return articleMapper.selectById(id);
    }
}